package ex06.pyrmont.core;

import javax.servlet.http.HttpServletRequest;

import org.apache.catalina.Container;
import org.apache.catalina.HttpRequest;
import org.apache.catalina.Mapper;
import org.apache.catalina.Request;
import org.apache.catalina.Wrapper;

/**
 * 和第五章的差不多;
 */
public class SimpleContextMapperV6 implements Mapper {

    /**
     * The Container with which this Mapper is associated.
     */
    private SimpleContextV6 context = null;

    public Container getContainer() {
        return (context);
    }

    public void setContainer(Container container) {
        if (!(container instanceof SimpleContextV6)) {
            throw new IllegalArgumentException("Illegal type of container");
        }
        context = (SimpleContextV6) container;
    }

    public String getProtocol() {
        return null;
    }

    public void setProtocol(String protocol) {
    }


    /**
     * Return the child Container that should be used to process this Request,
     * based upon its characteristics.  If no such child Container can be
     * identified, return <code>null</code> instead.
     * <br> 根据请求的特征，返回应用于处理此请求的子容器 ( 其实就是 servlet 类对象)。如果没有这样的  子容器可以被识别，则返回null。
     *
     * @param request Request being processed
     * @param update  Update the Request to reflect the mapping selection?
     * @throws IllegalArgumentException if the relative portion of the path cannot be URL decoded
     */
    public Container map(Request request, boolean update) {
        // Has this request already been mapped?
        if (update && (request.getWrapper() != null)) {
            return (request.getWrapper());
        }
        // Identify the context-relative URI to be mapped
        String contextPath = ((HttpServletRequest) request.getRequest()).getContextPath();
        String requestURI = ((HttpRequest) request).getDecodedRequestURI();
        String relativeURI = requestURI.substring(contextPath.length());
        // Apply the standard request URI mapping rules from the specification
        Wrapper wrapper = null;
        String servletPath = relativeURI;
        String pathInfo = null;
        String name = context.findServletMapping(relativeURI);
        if (name != null) {
            wrapper = (Wrapper) context.findChild(name);
        }

        // Update the Request (if requested) and return this Wrapper
        if (update) {
            request.setWrapper(wrapper);
            ((HttpRequest) request).setServletPath(servletPath);
            ((HttpRequest) request).setPathInfo(pathInfo);
        }
        return (wrapper);
    }
}
